package com.fm.practice;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.fm.practice.uitls.InputStreamFile;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import com.sun.xml.internal.ws.util.StreamUtils;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.*;

/**
 * @author: HeHui
 * @date: 2020-12-22 19:46
 * @description: csv工具类
 */
public class CsvUtil {

    public static final String CSV_DELIMITER = ",";
    private static final String NEW_LINE = System.lineSeparator();


    /**
     * 将多行记录，写入到输出流后面
     *
     * @param data   数据
     * @param output 输出
     */
    public static void appendToOutputForList(List<List<String>> data, OutputStream output) {
        assert output != null;
        try {
            write(data.parallelStream()
                    .filter(row -> !CollectionUtil.isEmpty(row))
                    .map(row -> CollectionUtil.join(row, CSV_DELIMITER))
                    .collect(Collectors.toList()), new BufferedWriter(new OutputStreamWriter(output, Charset.forName("UTF-8"))));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读
     *
     * @param inputStream 输入流
     * @return {@link List<String>}
     */
    private static List<String> read(InputStream inputStream) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        List<String> data = new ArrayList<>();
        String temp;
        try {
            while ((temp = reader.readLine()) != null) {
                data.add(temp);
            }
            return data;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 写
     *
     * @param data   数据
     * @param writer 作家
     */
    private static void write(List<String> data, Writer writer) {
        if (CollectionUtil.isEmpty(data)) {
            return;
        }
        try {
            for (String row : data) {
                //增加bom 防止 csv打开乱码
                writer.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
                writer.write(row);
                writer.write(NEW_LINE);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) throws IOException {
        ByteOutputStream outputStream = new ByteOutputStream();
        List<String> t = new ArrayList<>();
        t.add("标题1");
        t.add("标题2");
        t.add("标题3");
        List<String> a = new ArrayList<>();
        a.add("哈哈");
        a.add("1");
        a.add("就是把咖啡");
        List<List<String>> list = new ArrayList<>();
        list.add(t);
        list.add(a);
        list.add(a);
        list.add(a);
        list.add(a);
        list.add(a);
        list.add(a);
        list.add(a);

        /**
         * 把数据写入输出流
         */
        appendToOutputForList(list, outputStream);


        String filePath = "E:\\data/xuanxuan.zip";
        byte[] bytes = compressBytes(outputStream.getBytes(), "xuanxuan.csv", "123");

        //转为输入流
        InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

        //校验
        FileUtil.writeFromStream(byteArrayInputStream, filePath);
        System.out.println("ok！！---------------------------");
    }


    /**
     * @param bytes 数据
     * @param entryName 文件名称
     * @param passWord 密码
     * @return
     * @throws IOException
     */
    public static byte[] compressBytes(byte[] bytes, String entryName,
                                       String passWord) throws IOException {
        ByteArrayOutputStream bout = null;
        ByteArrayInputStream bin = null;
        try {
            //二进制数组输出流
            bout = new ByteArrayOutputStream();
            bin = new ByteArrayInputStream(bytes.clone());
            AesZipFileEncrypter encrypter;
            encrypter = new AesZipFileEncrypter(bout, new AESEncrypterBC());
            encrypter.add(entryName, bin, passWord);
            encrypter.close();
            return bout.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bout.close();
            bin.close();
        }
        return null;
    }







}
